package com.hong.servicehosp.controller.api;

import com.hong.commonutil.result.Result;
import com.hong.model.model.hosp.Department;
import com.hong.model.model.hosp.Schedule;
import com.hong.model.vo.hosp.DepartmentQueryVo;
import com.hong.model.vo.hosp.ScheduleQueryVo;
import com.hong.servicehosp.service.HospitalSetService;
import com.hong.servicehosp.service.ScheduleService;
import com.hong.serviceutil.common.helper.HttpRequestHelper;
import io.swagger.annotations.ApiOperation;
import io.swagger.annotations.ApiParam;
import org.apache.ibatis.annotations.Delete;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.domain.Page;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import java.util.Map;

@RequestMapping("/api/hosp")
@RestController
public class ApiScheduleController {
    @Autowired
    private HospitalSetService hospitalSetService;
    @Autowired
    private ScheduleService scheduleService;

    @ApiOperation("上传排班")
    @PostMapping("saveSchedule")
    public Result saveSchedule(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> map = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(map);
        scheduleService.save(paramMap);
        return Result.ok();
    }

    @ApiOperation("查询排班")
    @PostMapping("schedule/list")
    public Result findSchedule(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号
        String hostCode = (String) paramMap.get("hoscode");
        //科室编号
        String depCode = (String) paramMap.get("depcode");
        String pNumS = (String) paramMap.get("page");
        String pSizeS = (String) paramMap.get("limit");
        Integer pNum = pNumS == null ? 1 : Integer.valueOf(pNumS);
        Integer pSize = pSizeS == null ? 1 : Integer.valueOf(pSizeS);
        ScheduleQueryVo scheduleQueryVo = new ScheduleQueryVo();
        scheduleQueryVo.setHoscode(hostCode);
        scheduleQueryVo.setDepcode(depCode);
        Page<Schedule> pageModel = scheduleService.findPageSchedule(pNum, pSize, scheduleQueryVo);
        return Result.ok(pageModel);
    }

    @ApiOperation("删除排班")
    @PostMapping("schedule/remove")
    public Result removeSchedule(HttpServletRequest request) {
        //获取传递过来的医院信息
        Map<String, String[]> requestMap = request.getParameterMap();
        Map<String, Object> paramMap = HttpRequestHelper.switchMap(requestMap);
        //医院编号
        String hostCode = (String) paramMap.get("hoscode");
        //科室编号
        String scheduleCode = (String) paramMap.get("hosScheduleId");
        //签名校验
        String hospSign = (String) paramMap.get("sign");
        hospitalSetService.checkHospSign(hostCode, hospSign);
        scheduleService.remove(hostCode, scheduleCode);
        return Result.ok();
    }
    @ApiOperation("获取排班具体数据")
    @GetMapping("auth/findScheduleList/{hoscode}/{depcode}/{workDate}")
    public Result findScheduleList(
            @ApiParam(name = "hoscode", value = "医院code", required = true) @PathVariable String hoscode,
            @ApiParam(name = "depcode", value = "科室code", required = true) @PathVariable String depcode,
            @ApiParam(name = "workDate", value = "排班日期", required = true) @PathVariable String workDate) {
        return Result.ok(scheduleService.getDetailSchedule(hoscode, depcode, workDate));
    }
    @ApiOperation("根据排班id获取排班数据")
    @GetMapping("hospital/getSchedule/{scheduleId}")
    public Result getSchedule(@PathVariable String scheduleId){
        Schedule schedule = scheduleService.getScheduleById(scheduleId);
        return Result.ok(schedule);
    }
}
